package net.xisberto.bustime;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class BusTimeActivity extends CustomActivity implements OnClickListener, LocationListener {
	private LocationManager locManager;
	private Location loc = null;
	public static final int
		REQUEST_REGISTER = 1,
		REQUEST_SEARCH = 2,
		REQUEST_MANAGE = 3,
		REQUEST_VIEW_RECORD = 4;
	public static final String
		VIEW_TYPE = "view_type",
		BUS_ID = "bus_id",
		REQUEST_CODE = "request_code",
		RECORD_TIME = "record_time",
		RECORD_LATITUDE = "record_latitude",
		RECORD_LONGITUDE = "record_longitude";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_main);
        this.title.setText(R.string.app_name);
        
        findViewById(R.id.btn_manage_list).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);
        findViewById(R.id.btn_view_registers).setOnClickListener(this);

    }

	@Override
	protected void onStart() {
		super.onStart();
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Log.i(getApplication().getPackageName(), "Listening location");
	}

	@Override
	protected void onStop() {
		super.onStop();
		locManager.removeUpdates(this);
		Log.i(getApplication().getPackageName(), "Stop listening location");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i(getApplication().getPackageName(), "Destroy application");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_REGISTER:
			if (resultCode == Activity.RESULT_OK) {
				Toast.makeText(this, R.string.toast_saved_register, Toast.LENGTH_SHORT).show();
			}
			break;
		case REQUEST_SEARCH:
			if (resultCode == Activity.RESULT_OK) {
				Toast.makeText(this, R.string.toast_added_personal, Toast.LENGTH_SHORT);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(findViewById(R.id.btn_manage_list))) {
			Intent intent = new Intent(this, ManageListActivity.class);
			startActivityForResult(intent, REQUEST_MANAGE);
		} else if (v.equals(findViewById(R.id.btn_register))){
			Intent intent = new Intent(this, PersonalListActivity.class);
			if (loc == null) {
				alertDialogGPS();
			} else {
				intent.putExtra(RECORD_LATITUDE, loc.getLatitude());
				intent.putExtra(RECORD_LONGITUDE, loc.getLongitude());
				intent.putExtra(RECORD_TIME, loc.getTime());
				intent.putExtra(REQUEST_CODE, REQUEST_REGISTER);
				startActivityForResult(intent, REQUEST_REGISTER);
			}
		} else if (v.equals(findViewById(R.id.btn_view_registers))) {
			Intent intent = new Intent(this, ViewRecordsActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public void onLocationChanged(Location arg0) {
		Log.i(getApplicationContext().getPackageName(), "Location: "+arg0.toString());
		loc = arg0;
	}

	@Override
	public void onProviderDisabled(String provider) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}
	
	private void alertDialogGPS() {
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					dialog.cancel();
					break;
				default:
					dialog.cancel();
					break;
				}
			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.msg_location_null)
			.setPositiveButton(android.R.string.yes, dialogClickListener)
			.setNegativeButton(android.R.string.no, dialogClickListener)
			.show();
	}
}